package dbhandler;

import evthandler.EventMessage;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class Booking {

    private int bookingNo;
    private Room room;
    private Guest guest;
    private String inDate;
    private String outDate;
    private boolean confirmed;
    private Date checkInDate;
    private Date checkOutDate;
    private boolean closed;
    private Calendar cal = Calendar.getInstance();

    /**
     *
     */
    public Booking() {
    }

    /**
     * Creates the booking object
     * @param bookingNo Representing the booking Number
     */
    public Booking(int bookingNo) {
        this.bookingNo = bookingNo;
        int guestNo = 0;
        String roomNo = "";
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT * FROM booking WHERE bookingNo=" + bookingNo);
            while (rs.next()) {
                inDate = rs.getString("inDate");
                outDate = rs.getString("outDate");
                confirmed = rs.getBoolean("confirmed");
                closed = rs.getBoolean("closed");
                guestNo = rs.getInt("guestNo");
                roomNo = rs.getString("roomNo");
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in Booking", "The program could not\nget the needed information\nfrom the database").showException(ex);
        }
        guest = new Guest(guestNo);
        room = new Room(roomNo);
    }

    /**
     * Accessor method to receive whether or not the booking is ended.
     * @return boolean
     */
    public boolean isEnded() {
        return closed;
    }

    /**
     * Method to convert room number into booking number.
     * @param roomNo
     * @param today
     * @return bookingNo
     * @throws SQLException
     */
    public int roomNo2bookingNo(String roomNo, String today) {
        int bookingno = 0;
        try {
            ResultSet rs1 = DbCon.getInstance().executeQuery("SELECT bookingNo FROM booking WHERE roomNo=" + roomNo + " AND outDate='" + today + "'");
            while (rs1.next()) {
                bookingno = rs1.getInt("bookingNo");
            }
        } catch (SQLException ex) {
            new EventMessage("Problem !", "The system has not found any positive matches on the room number and today").showMessage();
        }
        return bookingno;
    }
    /*
     * Method to make an ArrayList<Date> with Date objects corresponding
     * to every intermediate date between the two chosen dates.
     *
     * @param inDate
     * @param outDate
     * @return ArrayList of Date
     */
    public ArrayList<Date> getIntermediateDays(String inDate, String outDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        ArrayList<Date> dateList = new ArrayList<Date>();
        try {
            checkInDate = (Date) sdf.parse(inDate);
            checkOutDate = (Date) sdf.parse(outDate);
        } catch (ParseException ex) {
            new EventMessage("Parse error in Booking.getIntermediateDays()", "There was an error parsing the dates\n").showException(ex);
        }

        cal.setTime(checkInDate);
        do {
            dateList.add(checkInDate);
            cal.add(Calendar.DATE, 1);
            checkInDate = (Date) cal.getTime();
        } while (checkInDate.before(checkOutDate));
        dateList.add(checkOutDate);

        return dateList;
    }

    /**
     * Method for deleting the booking from the database
     */
    public void delete() {
        try {
            DbCon.getInstance().executeUpdate("UPDATE booking SET closed=1 WHERE bookingNo='" + bookingNo + "'");
        } catch (SQLException ex) {
            new EventMessage("SQL error in Booking.delete()", "There was an error performing the sql inquery\n").showException(ex);
        }
    }

    /**
     * Sets all global booking variables to the database
     */
    public void update() {
        try {
            String sql = "UPDATE booking SET roomNo='" + room.getRoomNo() + "',guestNo=" + guest.getGuestNo() + ",inDate='" + inDate + "',outDate='" + outDate + "',confirmed=" + confirmed + " WHERE bookingNo=" + bookingNo;
            DbCon.getInstance().executeUpdate(sql);
        } catch (SQLException ex) {
            new EventMessage("SQL error in Booking", "There was an error performing the sql Update\n").showException(ex);
        }
    }

    /**
     * Method to insert information into the current object-information into
     * the database.
     * @return boolean
     */
    public boolean insert() {
        boolean bool = false;
        try {
            DbCon.getInstance().executeUpdate("INSERT INTO booking (roomNo, guestNo, inDate, outDate, confirmed) VALUES (" + room.getRoomNo() + ", " + guest.getGuestNo() + ", '" + inDate + "', '" + outDate + "', 0)");
            bool = true;
        } catch (SQLException ex) {
            new EventMessage("SQL error in Booking", "There was an error performing the sql insert\n").showException(ex);
            bool = false;
        }
        return bool;
    }

    /**
     * Accessor method to get the booking number.
     * @return int
     */
    public int getBookingNo() {
        return bookingNo;
    }

    /**
     * Accessor method to get the boolean of whether or not the booking
     * is confirmed.
     * @return boolean
     */
    public boolean isConfirmed() {
        return confirmed;
    }
    /**
     * Mutator method to set the boolean confirned.
     */
    public void setConfirmed(boolean confirmed){
        this.confirmed = confirmed;
    }

    /**
     * Accessor method to get the guest object.
     * @return Guest object.
     */
    public Guest getGuest() {
        return guest;
    }

    /**
     * Accessor method to get the check in date.
     * @return Formatted string
     */
    public String getInDate() {
        return inDate;
    }

    /**
     * Accessor method to get the check out date.
     * @return Formatted string
     */
    public String getOutDate() {
        return outDate;
    }

    /**
     * Accessor metod to get the room object.
     * @return Room object
     */
    public Room getRoom() {
        return room;
    }

    /**
     * Mutator method to set the checkin date
     * @param inDate
     */
    public void setInDate(String inDate) {
        this.inDate = inDate;
    }

    /**
     * Mutator method to set the checkout date
     * @param outDate
     */
    public void setOutDate(String outDate) {
        this.outDate = outDate;
    }

    /**
     * Mutator method to set the Room object to another.
     * @param room
     */
    public void setRoom(Room room) {
        this.room = room;
    }

    /**
     * Mutator method to set the Guest object to another.
     * @param guest
     */
    public void setGuest(Guest guest) {
        this.guest = guest;
    }
}
